package com.pxmeta.dynamic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.common.core.redis.RedisCache;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.entity.Mail;
import com.pxmeta.dynamic.entity.enums.MailTypeEnum;
import com.pxmeta.dynamic.mapper.MailMapper;
import com.pxmeta.dynamic.service.IMailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-08-17
 */
@Service
@RequiredArgsConstructor
public class MailServiceImpl extends ServiceImpl<MailMapper, Mail> implements IMailService {

    private final RedisCache redisCache;
    private static final String SENT_MESSAGE_KEY = "prod:sendInteractMessage:%s:%s:%s:%s";

    @Override
    public void sendInteractMessage(Integer type, Long userId, Long targetUserId, String message, Dynamic dynamic) {


        Long dynamicId = 0L;
        Mail mail = new Mail();
        if (null != dynamic){
            dynamicId = dynamic.getId();
            mail.setCircleId(dynamic.getCircleId());
            mail.setTargetId(dynamicId);
            mail.setContent(message);
            String imgUrls = dynamic.getImgUrls();
            if (StringUtils.isNotBlank(imgUrls)){
                String card = imgUrls.split(",")[0];
                mail.setCardContent(card);
            }else {
                mail.setCardContent(dynamic.getContent());
            }
        }

        if (type != 1){
            String key = String.format(SENT_MESSAGE_KEY, type, userId, targetUserId, dynamicId);

            Boolean flag = redisCache.getCacheObject(key);
            if (null == flag){
                redisCache.setCacheObject(key,Boolean.TRUE,1, TimeUnit.HOURS);
            }else {
                //防止重复推送
                return;
            }
        }



        mail.setTargetUserId(targetUserId);
        mail.setUserId(userId);
        if (targetUserId.equals(userId)){
            //跳过自己
            return;
        }
        mail.setType(MailTypeEnum.INTERACT.getType());
        mail.setInteractiveType(type);
        mail.setIsView(0);
        this.save(mail);
    }


    @Override
    public void sendInteractFollowMessage(Integer type, Long userId, Long targetUserId, Long circleId) {
        Mail mail = new Mail();
        mail.setCircleId(circleId);
        mail.setTargetUserId(targetUserId);
        mail.setUserId(userId);
        mail.setType(MailTypeEnum.INTERACT.getType());
        mail.setInteractiveType(type);
        mail.setIsView(0);
        this.save(mail);
    }
    @Override
    public void sendNoticeMessage(Long targetUserId, Long circleId, String message) {
        Mail mail = new Mail();
        mail.setTargetUserId(targetUserId);
        mail.setCircleId(circleId);
        mail.setContent(message);
        mail.setType(MailTypeEnum.NOTICE.getType());
        mail.setIsView(0);
        this.save(mail);
    }

    @Override
    public Long getCount(Long userId, Long circleId, Integer type) {
        LambdaQueryWrapper<Mail> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Mail::getCircleId, circleId)
                .eq(Mail::getIsView,0)
                .eq(Mail::getTargetUserId, userId);
        if (type != 0){
            wrapper.eq(Mail::getType, type);
        }
        return this.count(wrapper);
    }
}
